package com.net.xpay.core.helper.sms.chuanglan.chuanglan;

import com.alibaba.fastjson.JSON;
import com.net.xpay.core.helper.sms.chuanglan.chuanglan.clsms.request.SmsSendRequest;
import com.net.xpay.core.helper.sms.chuanglan.chuanglan.clsms.response.SmsSendResponse;
import com.net.xpay.core.helper.sms.chuanglan.chuanglan.clsms.util.ChuangLanSmsUtil;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ChuanglanSendUtil {
    public static final Logger LOGGER = LoggerFactory.getLogger(ChuangLanSmsUtil.class);

    /***
     * 创蓝-验证码
     */
    private static String _CL_SEND_URL = "http://smssh1.253.com/msg/v1/send/json";
//    private static String _CL_USER_NAME = "YZM1764618";
//    private static String _CL_PASSWD = "j73Ci6ompMf627";

    private static String _CL_USER_NAME = "YZM4874691";
    private static String _CL_PASSWD = "v2hnn05wDBbb22";


    /***
     * 创蓝-通知
     */
    private static String _CL_NORMAL_USER_NAME = "N0459107";
    private static String _CL_NORMAL_PASSWD = "T4J2UJuMI83484";

    public ChuanglanSendUtil() {

    }

    //创蓝验证短信
    public int sendMsgByCL(String mobile, String content, int type) {
//		try {
//			content = URLEncoder.encode(content, "utf-8");
//
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
        SmsSendRequest smsSingleRequest = null;
        if (type == 0) {
            smsSingleRequest = new SmsSendRequest(_CL_USER_NAME, _CL_PASSWD, content, mobile);
        } else if (type == 1) {
            smsSingleRequest = new SmsSendRequest(_CL_NORMAL_USER_NAME, _CL_NORMAL_PASSWD, content, mobile);
        }

        String requestJson = JSON.toJSONString(smsSingleRequest);

        LOGGER.info("before request string is: " + requestJson);

        String response = ChuangLanSmsUtil.sendSmsByPost(_CL_SEND_URL, requestJson);

        LOGGER.info("response after request result is :" + response);

        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
        return Integer.valueOf(smsSingleResponse.getCode());
    }


    public static void main(String[] args) throws HttpException, IOException {
        ChuanglanSendUtil util = new ChuanglanSendUtil();
//        System.out.println(util.sendMsgByCL("13777443718","您的验证码是3399。如非本人操作，请忽略本短信",0));
		System.out.println(util.sendMsgByCL("15207400311","您的验证码是1119。如非本人操作，请忽略本短信",0));
    }
}
